import navTo, { navToWebView } from '@/utils/nav-to'

const pageOptions = [
  { label: '首页', path: '/pages/main/index' },
  { label: '个人中心', path: '/pages/main/mine' },
  { label: '购物车', path: '/pages/main/cart' },
  { label: '分类', path: '/pages/main/category' },
  { label: '消息', path: '/pages/news/news-center' },
  { label: '登录', path: '/pages/user/login' },
  { label: '社区', path: '/pages/main/community' },
  { label: '积分商城首页', path: '' },
  { label: '我的订单', path: '/pages/my-order/list' },
  { label: '我的钱包', path: '/pages/my-wallet/index' },
  { label: '门店导购', path: '/pages/my-store-guide/index' },
  { label: '我的团', path: '/pages/hermes/my-collage' },
  { label: '我的抽奖', path: '/pages/activity/my-prize' },
  { label: '我的砍价', path: '/pages/hermes/my-bargain' },
  { label: '地址管理', path: '/pages/my-address/index' },
  { label: '我的咨询', path: '/pages/my-consult/list' },
  { label: '我的问答', path: '/pages/my-qa/list' },
  { label: '我的评价', path: '/pages/my-comment/center' },
  { label: '发票中心', path: '/pages/my-invoice/index' },
  { label: '我的优惠码', path: '' },
  { label: '试用活动报告页', path: '/pages/hermes/trial-report' },
  { label: '我的试用', path: '/pages/hermes/my-trial' },
  { label: '拼团页', path: '/pages/hermes/collage' },
  { label: '砍价页', path: '/pages/hermes/bargain' },
  { label: '礼品卡', path: '/pages/my-gift-card/index' },
  { label: '提货卡', path: '/pages/my-picking-card/index' },
  { label: '秒杀页', path: '/pages/hermes/seckill' },
  { label: '试用页', path: '/pages/hermes/trial' },
  { label: '邀请送礼', path: '' },
  { label: '签到', path: '/pages/my-sign/index' },
  { label: '直播广场', path: '/pages/live/area' },
  { label: '动画', path: '/pages/live/animation' },
  { label: '积分商城分类', path: '' },
  { label: '店铺首页', path: '/pages/store/index' },
  { label: '店铺分类', path: '/pages/store/category' },
  { label: '客服', path: '/pages/my-sobot/index' },
  { label: '店铺全部商品列表', path: '/pages/store/product' },
  { label: '商品收藏', path: '/pages/product/collection' },
  { label: '店铺收藏', path: '/pages/store/collection' },
  { label: '浏览足迹', path: '/pages/my-footprint/index' },
  { label: '我喜欢的', path: '/pages/post/collection' },
  { label: '定位', path: '/pages/position/index' },
  { label: '领券中心', path: '/pages/coupon/center' },
]

function getParam(url, name) {
  try {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
    var r = url.split('?')[1].match(reg)
    if (r != null) {
      return r[2]
    }
    return ''
  } catch (e) {
    return ''
  }
}
const linkUrl = (params, query = {}) => {
  if (!params || Object.keys(params).length === 0) return
  switch (+params.type) {
    case 1:
      if (params.data === '') return
      // 如果是大转盘 砸金蛋 摇一摇
      if (params.data.indexOf('activity/lucky-turntable') > -1) {
        let hermesId = getParam(params.data, 'hermesId')
        navTo('/pages/activity/lucky-turntable', { hermesId })
        return
      } else if (params.data.indexOf('activity/golden-eggs') > -1) {
        let hermesId = getParam(params.data, 'hermesId')
        navTo('/pages/activity/golden-eggs', { hermesId })
        return
      } else if (params.data.indexOf('activity/shake-it') > -1) {
        let hermesId = getParam(params.data, 'hermesId')
        navTo('/pages/activity/shake-it', { hermesId })
        return
      }
      navToWebView(params.data)
      break
    case 2:
      navTo('/pages/product/detail', { mpId: params.ref[0].mpId })
      break
    case 3:
      navTo('/pages/search/result', { keyword: params.data })
      break
    case 5:
      navTo('/pages/search/result', {
        navCategoryIds: params.ref[0].id,
        pageType: 8,
      })
      break
    case 4:
      const current = pageOptions.find((item) => item.label === params.data)

      if (/^\/pages\/store/.test(current.path)) {
        current.storeId = uni.getStorageSync('storeId')
      }

      if (!current) {
        uni.showToast({
          title: '页面正在开发中...',
          icon: 'none',
        })
        return
      }

      navTo(current.path, { ...current, ...query })
      break
    case 6:
      let pageInfo = params.ref[0]
      if (pageInfo.type === 8) {
        navTo('/pages/community/message-detail', { id: pageInfo.id })
      } else if (pageInfo.type === 2 || pageInfo.type === 12) {
        navTo('/pages/example/marketing', { id: pageInfo.id, type: pageInfo.type })
      } else {
        uni.showToast({
          title: '页面正在开发中...',
          icon: 'none',
        })
      }
      break
    default:
      uni.showToast({
        title: '系统开小差了',
        icon: 'none',
      })
      break
  }
}

export default linkUrl
